package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.foundations.dto.response.ConfigRegionInnerResDTO;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.JsonUtils;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.*;
import com.jzo2o.foundations.model.domain.ConfigRegion;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ConfigRegionSetReqDTO;
import com.jzo2o.foundations.model.dto.response.ConfigRegionResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.service.*;
import nonapi.io.github.classgraph.json.JSONUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 区域业务配置 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-08-21
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private IRegionService regionService;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    private IServeItemService  serveItemService;

    @Resource
    private IServeService serveService;

//    RedisConstants
    @Override
    @Caching(cacheable = {
            //result为null时,属于缓存穿透情况，缓存时间30分钟
            @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE,
                    key = "#regionId",
                    unless = "#result.size() != 0",
                    cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            //result不为null时,永久缓存
            @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE,
                    key = "#regionId",
                    unless = "#result.size() == 0",
                    cacheManager = RedisConstants.CacheManager.FOREVER)
    })

    public List<ServeAggregationTypeSimpleResDTO> serveTypeList(Long regionId) {


        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.equal(region.getActiveStatus(),
                FoundationStatusEnum.DISABLE.getStatus())
                ||ObjectUtil.isNull(region)) {
            return Collections.emptyList();
        }



        List<ServeAggregationTypeSimpleResDTO> list=
               regionService.serveTypeList(regionId);
        if (ObjectUtil.isNull(list)) {
            return Collections.emptyList();
        }

        return list;
    }

    @Override
    @Caching(
            cacheable = {
                    //result为null时,属于缓存穿透情况，缓存时间30分钟
                    @Cacheable(value = RedisConstants.CacheName.HOT_SERVE,
                            key = "#regionId",
                            unless = "#result.size() != 0",
                            cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
                    //result不为null时,永久缓存
                    @Cacheable(value = RedisConstants.CacheName.HOT_SERVE,
                            key = "#regionId",
                            unless = "#result.size() == 0",
                            cacheManager = RedisConstants.CacheManager.FOREVER)
            }
    )
    public List<ServeAggregationSimpleResDTO> hotServeList(Long regionId) {

        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isNull(region)|| ObjectUtil.equals(region.getActiveStatus(),
                FoundationStatusEnum.DISABLE.getStatus())) {
            return Collections.emptyList();
        }


        List<ServeAggregationSimpleResDTO> list=
                regionService.hotServeList(regionId);
        if (ObjectUtil.isNull(list)) {
            return Collections.emptyList();
        }
        return list;

    }

    @Override
    public ServeAggregationSimpleResDTO detailServe(Long id) {
        Serve serve = serveService.selectByIdCache(id);

        Long serveItemId = serve.getServeItemId();

        ServeItem serveItem = serveItemService.selectByIdCache(serveItemId);


        ServeAggregationSimpleResDTO serveAggregationSimpleResDTO =
                BeanUtil.toBean(serve, ServeAggregationSimpleResDTO.class);

        serveAggregationSimpleResDTO.setServeItemName(serveItem.getName());
        serveAggregationSimpleResDTO.setServeItemImg(serveItem.getImg());
        serveAggregationSimpleResDTO.setDetailImg(serveItem.getDetailImg());
        serveAggregationSimpleResDTO.setUnit(serveItem.getUnit());
        return serveAggregationSimpleResDTO;


    }
}
